package com.yx.springai.chatclient.entity;

import java.time.Duration;
import java.time.LocalDate;
import java.util.Objects;

/**
 * Song实体类，表示音乐曲目
 * 包含歌曲的基本信息和相关操作
 */
public class Song {
    // 实例字段
    private String title;
    private String artist;
    private String album;
    private Duration duration;
    private LocalDate releaseDate;
    private String genre;
    private Integer trackNumber;
    private String composer;
    private String lyrics;
    private Integer playCount;
    private Double rating;

    // 构造方法
    public Song() {
        this.playCount = 0;
        this.rating = 0.0;
    }

    public Song(String title, String artist, Duration duration) {
        this();
        this.title = title;
        this.artist = artist;
        this.duration = duration;
    }

    public Song(String title, String artist, String album, Duration duration,
                LocalDate releaseDate, String genre, Integer trackNumber) {
        this(title, artist, duration);
        this.album = album;
        this.releaseDate = releaseDate;
        this.genre = genre;
        this.trackNumber = trackNumber;
    }


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public Duration getDuration() {
        return duration;
    }

    public void setDuration(Duration duration) {
        this.duration = duration;
    }

    public LocalDate getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(LocalDate releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public Integer getTrackNumber() {
        return trackNumber;
    }

    public void setTrackNumber(Integer trackNumber) {
        this.trackNumber = trackNumber;
    }

    public String getComposer() {
        return composer;
    }

    public void setComposer(String composer) {
        this.composer = composer;
    }

    public String getLyrics() {
        return lyrics;
    }

    public void setLyrics(String lyrics) {
        this.lyrics = lyrics;
    }

    public Integer getPlayCount() {
        return playCount;
    }

    public void setPlayCount(Integer playCount) {
        this.playCount = playCount;
    }

    public Double getRating() {
        return rating;
    }

    public void setRating(Double rating) {
        // 确保评分在0-5之间
        if (rating != null && (rating < 0.0 || rating > 5.0)) {
            throw new IllegalArgumentException("Rating must be between 0.0 and 5.0");
        }
        this.rating = rating;
    }

    // 业务方法
    public void incrementPlayCount() {
        this.playCount++;
    }

    public String getFormattedDuration() {
        if (duration == null) {
            return "00:00";
        }
        long seconds = duration.getSeconds();
        long minutes = seconds / 60;
        long remainingSeconds = seconds % 60;
        return String.format("%02d:%02d", minutes, remainingSeconds);
    }

    public boolean isFromAlbum(String albumName) {
        if (album == null || albumName == null) {
            return false;
        }
        return album.equalsIgnoreCase(albumName);
    }

    public boolean isByArtist(String artistName) {
        if (artist == null || artistName == null) {
            return false;
        }
        return artist.equalsIgnoreCase(artistName);
    }


    @Override
    public String toString() {
        return "Song{" +
                ", title='" + title + '\'' +
                ", artist='" + artist + '\'' +
                ", album='" + album + '\'' +
                ", duration=" + getFormattedDuration() +
                ", releaseDate=" + releaseDate +
                ", genre='" + genre + '\'' +
                ", trackNumber=" + trackNumber +
                ", playCount=" + playCount +
                ", rating=" + rating +
                '}';
    }

    // 静态工具方法
    public static Duration parseDuration(String durationString) {
        if (durationString == null || durationString.isEmpty()) {
            return Duration.ZERO;
        }

        try {
            String[] parts = durationString.split(":");
            if (parts.length == 2) {
                long minutes = Long.parseLong(parts[0]);
                long seconds = Long.parseLong(parts[1]);
                return Duration.ofSeconds(minutes * 60 + seconds);
            } else if (parts.length == 3) {
                long hours = Long.parseLong(parts[0]);
                long minutes = Long.parseLong(parts[1]);
                long seconds = Long.parseLong(parts[2]);
                return Duration.ofSeconds(hours * 3600 + minutes * 60 + seconds);
            } else {
                throw new IllegalArgumentException("Invalid duration format: " + durationString);
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid duration format: " + durationString, e);
        }
    }
}